package com.lz.myblog.controller;


import com.lz.myblog.entity.*;
import com.lz.myblog.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller //注解为请求控制处理
public class IndexController {
    @Autowired
    AdmininfoRepository admininfoRepository;
    @Autowired
    CategoryRepository categoryRepository;
    @Autowired
    ArticletypeRepository articletypeRepository;
    @Autowired
    WritingsRepository writingsRepository;
    @Autowired
    LiuyanRepository liuyanRepository;
    @Autowired
    UserinfoRepository userinfoRepository;
    @Autowired
    WeiyuRepository weiyuRepository;
    @Autowired
    PhotoinfoRepository photoinfoRepository;

//    @RequestMapping("/category")
//    public String catrgory(Model model){
//        List<Category> all = categoryRepository.findAll();
//        model.addAttribute("category",all);
//        return "admin/index";
//    }
@RequestMapping("/zhuce")
public String zhuce(Admininfo admininfo){

    admininfoRepository.save(admininfo);
    return "/admin/login";
}

    @RequestMapping("/liuyanadd")
    public String save(Liuyaninfo liuyaninfo){
        System.out.println("liuyaninfo = " + liuyaninfo);
        liuyanRepository.save(liuyaninfo);
        return "redirect:indexliuyan";
    }


    @RequestMapping("/aihaoadd")
    public String save1(Category category){
        categoryRepository.save(category);
        return "redirect:aihaolist";
    }


    @RequestMapping("/search")
    public String search(Model model ,String keyword){
        List<Writingsinfo> all = writingsRepository.findAllByWritingstitleContaining(keyword);
        model.addAttribute("all",all);
        return "searchList";
    }
    @RequestMapping("/login")
public String login(String username, String password, String vercode, Model model, HttpSession session) {
    //获取session中的验证码
    String sRand = session.getAttribute("sRand").toString();

    if (!sRand.equalsIgnoreCase(vercode)) {  //加感叹号验证码不相等
        model.addAttribute("msg", "验证码错误,请重试");
        return "login";
    }
    List<Userinfo> list = userinfoRepository.findByUsernameEquals(username);
    if (list.size() == 0) {   //判断用户名是否存在
        model.addAttribute("msg", "管理员用户名不存在");
        return "login";
    }
    Userinfo userinfo = list.get(0);
    if (userinfo.getUserpwd().equals(password)) {
        //登录成功
        //记录当前登录用户
        session.setAttribute("login", userinfo);
        System.out.println("userinfo = " + userinfo);
        return "redirect:/index";

    } else {
        model.addAttribute("msg", "管理员密码错误");
        return "login";
    }
}

    //系统默认进入页面
    @RequestMapping("/")
    public String shouye(){
        return "redirect:index";
    }


        @RequestMapping("/indexweiyu")
        public String indexweiyu(Model model,Integer p){
            int pageSize = 10; //每页显示10条数据
            p = p==null?1:p; //判断是否传递了页码
//        p= p-1;
            Sort sort = Sort.by(Sort.Direction.DESC,"weiyuid");  //排序条件
            Page<Weiyuinfo> all = weiyuRepository.findAll(PageRequest.of(p - 1, pageSize, sort));
            List<Weiyuinfo> content = all.getContent();

            int totalPages = all.getTotalPages();
            //绑定参数
            model.addAttribute("content",content);
            model.addAttribute("totalPages",totalPages);
            model.addAttribute("currenPage",p);
            return "weiyu";
        }
        @RequestMapping("/indexphoto")
        public String indexphoto(Model model,Integer p) {
            int pageSize = 10; //每页显示10条数据
            p = p == null ? 1 : p; //判断是否传递了页码
//        p= p-1;
            Sort sort = Sort.by(Sort.Direction.DESC, "photoid");  //排序条件
            Page<Photoinfo> all = photoinfoRepository.findAll(PageRequest.of(p - 1, pageSize, sort));
            List<Photoinfo> content = all.getContent();

            int totalPages = all.getTotalPages();
            //绑定参数
            model.addAttribute("content", content);
            model.addAttribute("totalPages", totalPages);
            model.addAttribute("currenPage", p);
            return "photo";
        }
        @RequestMapping("/indexliuyan")
        public String liebiao(Model model,Integer p) {
            int pageSize = 10; //每页显示10条数据
            p = p==null?1:p; //判断是否传递了页码
//        p= p-1;
            Sort sort = Sort.by(Sort.Direction.DESC,"liuyanid");  //排序条件
            List<Liuyaninfo> all1 = liuyanRepository.findAll();
            Page<Liuyaninfo> all = liuyanRepository.findAll(PageRequest.of(p - 1, pageSize, sort));
            List<Liuyaninfo> content = all.getContent();
            Integer a = all1.size();
            int totalPages = all.getTotalPages();
            //绑定参数
            model.addAttribute("content",content);
            model.addAttribute("totalPages",totalPages);
            model.addAttribute("currenPage",p);
            model.addAttribute("size" ,a);

            return "liuyan";
        }

        @RequestMapping("/details")

        public String details(Model model, Integer writingsid) {
            Writingsinfo one = writingsRepository.getOne(writingsid);
            model.addAttribute("one", one);
            return "details";
        }

        @RequestMapping("/index")
        public String index(Model model, HttpSession session, Integer writingsid,Integer p) {
            writingsid = writingsid == null ? 1 : writingsid;
            p = p == null ? 1 : p;
            int pageSize = 5; //每页显示10条数据
            Articletypeinfo one = articletypeRepository.getOne(writingsid);
            String writingstype = one.getArticletypename();
            Sort sort = Sort.by(Sort.Direction.DESC,"writingsid");  //排序条件
            List<Articletypeinfo> all1 = articletypeRepository.findAll();
            List<Writingsinfo> all2 = writingsRepository.findAllByWritingstypeContaining(writingstype);
            List<Writingsinfo> all3 = writingsRepository.findAllByWritingstypeContaining(writingstype, PageRequest.of(p - 1, pageSize));
            Integer a = all2.size();
            //方便多个页面调用   放在sess回话中
            session.setAttribute("returnid", writingsid);
            session.setAttribute("articletypeList", all1);
            session.setAttribute("writeid", all3);

            return "index";
        }
    }


